#include "SwfPro.h" 

SwfPro swfPro;

CBaseMc::CBaseMc()
:m_nMyFrame(0)
{
    
}

bool CBaseMc::updateMovieclip(){
    return false;
}

void CBaseMc::end(){
};

void CBaseMc::start(){
};




/***************************************************
 * TMovieItem
 **************************************************/

TMovieItem::TMovieItem()
:isPlay(false)
,m_pContainer(NULL)
,isLoop(false){
    m_pMainSwf=new CSwfDataVo;
    
}


void TMovieItem::clearCahe(){ 
    m_pMainSwf->clearCache();
    if(m_pContainer && m_pContainer->getParent()){
        m_pContainer->getParent()->removeChild(m_pContainer);
        m_pContainer=NULL;
    }
    
    m_pContainer=CSwfBitContainer::create();
    addChild(m_pContainer);
}

TMovieItem::~TMovieItem(){
    SAFE_DEL(m_pMainSwf);
}

bool  TMovieItem::init(){
    if(!TPanel::init()){
        return false;
        
    }
    
    m_pContainer=CSwfBitContainer::create();
    addChild(m_pContainer);
    return true;
}

void TMovieItem::setVisible(bool value){
    if(value){
        if(!isPlay){
            start();
        }
    }else{
        if(isPlay){
            SwfPro::shareSwfPro()->unRegSwf(this);
        }
    }
    TPanel::setVisible(value);
}

void TMovieItem::onEnter(){
    TPanel::onEnter();
    
    if(isVisible()){
        start();
    }
}

void TMovieItem::onExit(){
    TPanel::onExit();
    SwfPro::shareSwfPro()->unRegSwf(this);
}

void TMovieItem::setPath(const string& path){
    m_pMainSwf->setUrl(path);
}

bool TMovieItem::updateMovieclip(){
    if(!this->isVisible()){
        return false ;
    }
    
    if(isPause){
        return false;
    }
    if(!getParent()){
        return false;
    }
    if(m_nMyFrame>=m_nTotalFrame-1){
        if(isLoop){ 
            m_nMyFrame=0;
        }else{
            TPanel::setVisible(false);
            return true;
        }
        
    }else{
        m_nMyFrame++;
    }
    
    setBitVo( m_pMainSwf->getBitAndPos("stand", m_nMyFrame));
    
    return false;
}



void TMovieItem::setContainerBitVo(CSwfBitContainer* container, CSwfBitVo* bitVo){
    if(container==NULL){
        return ;
    }
    container->setSwfBitVo(bitVo); 
}


 

void TMovieItem::setBitVo(CSwfBitVo* bitVo){
    if(m_pContainer){
        setContainerBitVo(m_pContainer,bitVo);
    }
}

void TMovieItem::end(){
    isPlay=false;
}

void TMovieItem::start(){
    isPlay=true;
    isPause=false;
    m_nMyFrame=0;
    SwfPro::shareSwfPro()->regSwf(this);
}


void TMovieItem::initWidthConf(xml_node<> * pItem){
    TPanel::initWidthConf(pItem);
    
    m_nTotalFrame= readAttrInt(pItem, "frame");
    isLoop= readAttrInt(pItem, "loop")==1;
    string path;
    readAttrString(pItem, "source", path);
    setPath(path);
}



void TMovieItem::pause(int frame){
    m_nMyFrame=frame;
    setBitVo( m_pMainSwf->getBitAndPos("stand", m_nMyFrame));
    
    isPause=true;
}
void TMovieItem::continuePlay()
{
    isPause=false;
    
}



SwfPro* SwfPro::shareSwfPro(){
    return &swfPro;
}


SwfPro::SwfPro(){
     
}

SwfPro::~SwfPro(){ 
} 

void SwfPro::regSwf( CBaseMc* item){
    if(item==NULL){
        return;
    } 
    
    if(m_gDic.find(item)==m_gDic.end()){  
        m_gDic.insert(pair<CBaseMc* , int>(item,1)); 
    } 

}


void SwfPro::unRegSwf(CBaseMc* item){
    if(m_gDic.find(item)!=m_gDic.end()){
        m_gDic.erase(item);
        item->end(); 
    }

}

void SwfPro::updateMovieclip(){ 
    map<CBaseMc*,int> ::iterator it=m_gDic.begin(); 
    for( ; it!=m_gDic.end();){
        CBaseMc* mc=it->first;
        if(mc ){
            bool remove=  mc->updateMovieclip();
            if(remove){ 
                m_gDic.erase(it++); 
                mc->end();
            }else{
                ++it;
            }
        }
    }
     
}